package com.gooluke.game.strategy;

import com.gooluke.game.annotation.SupportUserType;
import com.gooluke.game.enums.UserType;
import com.gooluke.game.service.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author gooluke
 * description 客服策略类
 * datetime 2025-05-18 2:01
 */
@Component
public class CustomStrategy {

    /**
     * 将所有的策略都放入map中
     */
    private static Map<UserType, CustomService> customServiceMap;

    @Autowired
    public CustomStrategy(List<CustomService> customServiceList) {
        customServiceMap = customServiceList.stream().collect(Collectors.toMap(this::getUserType, Function.identity()));
    }

    /**
     * 获取用户类型
     * @param customService 实现了CustomService接口的类
     * @see  com.gooluke.game.enums.UserType
     * @return 用户类型
     */
    private UserType getUserType(CustomService customService) {
        SupportUserType supportUserType = customService.getClass().getAnnotation(SupportUserType.class);
        return supportUserType.type();
    }

    /**
     * 根据用户积分获取对应的客服
     * @param points 用户积分
     * @return 客服
     */
    public static CustomService getCustomService(int points) {
        return customServiceMap.get(UserType.of(points));
    }

}
